package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDouLuoDaLuEventHandle;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_HELP_OR_GET_REWARD_FROM_CROSS,desc = "协助/领取协助奖励")
public class DouLuoDaLuHelpActionCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        DouLuoDaLuProto.DouLuoDaLuHelpActionCrossRespMsg crossRespMsg = DouLuoDaLuProto.DouLuoDaLuHelpActionCrossRespMsg.parseFrom(packet.getBytes());
        DouLuoDaLuProto.DouLuoDaLuHelpActionRespMsg.Builder respMsg = crossRespMsg.getRespClient().toBuilder();
        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();
        ret = respMsg.getRet();
        if(ret == 0){
            // 加道具
            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            Property reward = PropertyHelper.parseStringToProperty(respMsg.getReward());
            if(reward != null){
                currencyModule.addCurrency(reward, eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuHelpActionReward);
            }
            if(respMsg.hasScore()){
                //第一阶段积分补加入背包
                //currencyModule.addCurrency(config.SCORE_ITEM_ID_FOREST + "=" + respMsg.getScore(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuHelpActionReward);
            }
            if(respMsg.hasAllScore()){
                player.getModule(DouLuoDaLuModule.class).updateConditionValue(respMsg.getAllScore(), eGamePlayerEventType.DouLuoDaLuPhaseOneUserScoreCrossRank.getValue());
            }
            if(crossRespMsg.hasLog() && respMsg.getIsKill()){
                DouLuoDaLuProto.DouLuoDaLuHelpActionLogEntity logEntity = crossRespMsg.getLog();

                //协助日志
                AutoLogMgr.add(new LogDouLuoDaLuEventHandle(DouLuoDaLuMgr.getActivityInfo().getActivityId(), player.getUserId(), logEntity.getTeamId(), logEntity.getRegionId(),
                        logEntity.getEventType(), logEntity.getEventId(), 4, logEntity.getTargetId(), logEntity.getEventTime(), logEntity.getStatus(),
                        logEntity.getBeforeAttackBlood(), respMsg.getRemainBlood(), respMsg.getDamage(), respMsg.getReward(), new ArrayList<>(), respMsg.getScore()));
            }
        }
        player.sendPacket(Protocol.U_DOU_LUO_DA_LU_HELP_OR_GET_REWARD, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
